﻿<?php

if(!defined('IN_PHPSCUP'))exit('hacking'); 
class AdApp extends BaseApp
{

    var $type = 'ad';
	
    function index()
    {

		$data = $this->get_ad_data();	
        $this->assign('data', $data);		
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		$this->assign('word', $word);
		$this->assign('v', $v);
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
		
        $this->display('ad_list.view.html');
    }
    function get_ad_data()
    {
		return @include (ROOT_PATH . '/data/ad/ad.php');			
    }

    function upfile()
    {
	 if(!isset($_POST["action"]))
	 {
	 $_POST["action"]='';
	 }
	 if(!isset($_GET['id']))
	 {
	 $_GET['id']='';
	 }
	 if(!isset($_GET["edit"]))
	 {
	 $_GET["edit"]='';
	 }	 
	 if(!isset($_GET["picurl"]))
	 {
	 $_GET["picurl"]='';
	 }
	 if(!isset($_GET["uppic"]))
	 {
	 $_GET["uppic"]='';
	 }	
	    $uppic = $_GET["uppic"];
        $this->assign('uppic', $uppic);	  			
		$action = $_POST["action"];
		
		$id = intval($_GET['id']);
		
		$edit = $_GET["edit"];
		$picurl = $_GET["picurl"];
		if($edit)
		{
        $this->assign('edit', $edit);		
		}
        if($picurl)
		{
        $this->assign('picurl', $picurl);
		}

						
		if($action == 'post')
		{
				if(!$_FILES['postfile1']['tmp_name']){
					$id || $id = intval($_POST['id']);	
					echo '<script> alert("请选择上传的图片");</script>';	
				}else{
	        if(!isset($_POST["edit"]))
	        {
	         $_POST["edit"]='';
	        }	 
	        if(!isset($_POST["picurl"]))
	        {
	         $_POST["picurl"]='';
	        }
	       if(!isset($_POST['id']))
	       {
	        $_POST['id']='';
	        }			
		       $edit = $_POST["edit"];
		       $picurl = $_POST["picurl"];
		 	   $id || $id = intval($_POST['id']);		

		       include(ROOT_PATH.'/lib/upfile.lib.php');	  
	           $upfile = new Upfile('ad/'.$id); 	  
	           $updata = $upfile->_batch_upfile('prot');		 

		       $newfile = $updata[1]['path'];
		       
			   if($edit=='editp' && $picurl)
			   {
			   
/*			          $TB_new = &$this->_model('prot');			    		 		 		
		              $TB_new->editpicurl($id,array('picurl'=>$newfile));*/
					  
		              $pic_dir= ROOT_PATH . '/' . UPFILE_PATH . '/' . $picurl;
			          if(!is_dir($pic_dir)&&file_exists($pic_dir))
		              {
		               del_file($pic_dir);	
		              }				   
			   }
			   
			    if($edit=='edit' && $picurl)
		       {
		              $pic_dir= ROOT_PATH . '/' . UPFILE_PATH . '/' . $picurl;
			          if(!is_dir($pic_dir)&&file_exists($pic_dir))
		              {
		               del_file($pic_dir);	
		              }				 
		       }
		 	   
			
		
		      $pic_url = SITE_URL . '/' . UPFILE_PATH . '/' .$newfile;

			 echo "<A HREF='?app=ad&act=upfile&edit=edit&picurl=$newfile&id=$id&uppic={$uppic}'>修改图片</A>";/*&nbsp;&nbsp;<a href='javascript:' style='color:red;' onClick=\"javascript:window.open('?app=ad&act=cutimg&nextpic=1&width=400&height=325&srcimg={$pic_url}&uppic={$uppic}','','width=580,height=530');\">[裁剪图片]</a>";*/
	     
		$jsfun="upfile";
	    echo "<script>
				if(self==top){
					window.opener.$jsfun('$newfile','{$uppic}');
					window.self.close();
				}else{
					window.parent.$jsfun('$newfile','{$uppic}');
				}
		     </script>";
				
	    exit;
		
			}
		}

        $this->assign('id', $id);
	    $this->assign('app', $this->type);					    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
        $this->assign('alldirpicurl',ROOT_PATH.'/'.UPFILE_PATH.'/'.$picurl);
		
        $this->assign('site_url',SITE_URL);
        $this->assign('edit',$edit);
        $this->assign('picurl',$picurl);		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
		
        $this->display('upfile.view.html');
    }



    function cutimg()
    {
	 if(!isset($_POST['action']))
	 {
	 $_POST['action'] = '';
	 }
     $action = $_POST['action'];
	 
    if($action=="cutimg")
	{
     $uploadfile = $_POST['uploadfile'];	
     $x = $_POST['x'];
     $y = $_POST['y'];	 
     $w = $_POST['w'];	 
     $h = $_POST['h'];	 
     $scale = $_POST['scale'];
     $rw = $_POST['rw'];	 
     $rh = $_POST['rh'];	 

	 if(!isset($_GET['post']))
	 {
	    $_GET['post'] = '';
	 }
	 $post = $_GET['post'];

	 
	$NewPic=str_replace(SITE_URL.'/'.UPFILE_PATH.'/',"",$uploadfile);
	$NewPic=ROOT_PATH.'/'.UPFILE_PATH.'/'.$NewPic;
	$pathinfo = pathinfo(basename($NewPic));
//	$NewPic = str_replace("_old",'',$NewPic);
		
	if(!getimagesize($NewPic)){
		die("图片有误!!");
	}
    @include_once(ROOT_PATH ."/lib/image.lib.php");
	

	 if($post=='gai')
	 {	
	    $oldpic = str_replace($pathinfo['filename'],$pathinfo['filename']."_old",$NewPic);	 
		if(!strstr($oldpic, '_old_old')&&!is_file($oldpic))
		{
	     copy($NewPic,$oldpic);	
		} 
	    $oldpic = $NewPic;	
	 }
	 else
	 {
	  $oldpic = str_replace($pathinfo['filename'],$pathinfo['filename']."_old",$NewPic);	 
	  copy($NewPic,$oldpic);	 
	 }	
    if($post=='gai')
	{
	$oldpic1 = str_replace("_old",'',$NewPic);	
	Image::cutimg($NewPic,$oldpic1,$x,$y,$rw,$rh,$w,$h,$scale);		
	}
	else
	{
	Image::cutimg($NewPic,$NewPic,$x,$y,$rw,$rh,$w,$h,$scale);	
	}

	
	$oldpic = str_replace(ROOT_PATH.'/'.UPFILE_PATH.'/',"",$oldpic);
	//$oldpic	= SITE_URL.'/'.UPFILE_PATH.'/'.$oldpic ;
	$pic1="$uploadfile";
	$pic1 = str_replace("_old",'',$pic1);		
	echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
	die("剪裁成功,你可以点击查看截图效果:<br><A HREF='$pic1' target=_blank>样式</A><br> <a href='?app=ad&act=cutimg&nextpic=1&width=400&height=325&post=gai&srcimg={$pic1}'>从新剪裁</a><br> <a href='javascript:window.self.close()'>点击关闭</a>");
	exit;
    }
		$height= $_GET['height'];		
		$width= $_GET['width'];	
		$nextpic= $_GET['nextpic'];			
		$srcimg= $_GET['srcimg'];
		$tempsrcimg = $srcimg;
	   if(!isset($_GET['post']))
	   {
	    $_GET['post'] = '';
	   }
	   $post = $_GET['post'];
	   if($post=='gai')
	   {
	   	$srcimg=str_replace(SITE_URL.'/'.UPFILE_PATH.'/',"",$srcimg);
		$srcimg = str_replace("_old",'',$srcimg);			 
	   	$pathinfo = pathinfo(basename($srcimg));
	
		$srcimg = str_replace($pathinfo['filename'],$pathinfo['filename']."_old",$srcimg);
		if(is_file(ROOT_PATH.'/'.UPFILE_PATH.'/'.$srcimg))
		{
		$srcimg = SITE_URL.'/'.UPFILE_PATH.'/'.$srcimg;		
		}
		else
		{
		$srcimg = $tempsrcimg;	
		}

	   }
        $this->assign('tempsrcimg', $tempsrcimg);		   	
        $this->assign('height', $height);					
        $this->assign('width', $width);
        $this->assign('nextpic', $nextpic);		
        $this->assign('srcimg', $srcimg);		
        $this->assign('post', $post);
						
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
		
        $this->display('cutimg.view.html');     

    }




    function edit()
    {	
		$id= intval($_GET['id']);
		
		$tempdata = $this->get_ad_data();	
        $data = $tempdata[$id];

        $this->assign('data', $data);
        $this->assign('id', $id);
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
		
        $this->display('ad_edit.view.html');   
    }





    function editpost()
    {
		$id= intval($_POST['id']);
		$data = $_POST['data'];
		if(!isset($data['name']))$data['name']='';
		if(!isset($data['demourl']))$data['demourl']='';
		if(!isset($data['left']['picurl']))$data['left']['picurl']='';		
		if(!isset($data['left']['url']))$data['left']['url']='';
		if(!isset($data['left']['height']))$data['left']['height']='0';
		if(!isset($data['left']['width']))$data['left']['width']='0';
		
		if(!isset($data['right']['picurl']))$data['right']['picurl']='';		
		if(!isset($data['right']['url']))$data['right']['url']='';
		if(!isset($data['right']['height']))$data['right']['height']='0';
		if(!isset($data['right']['width']))$data['right']['width']='0';		
	
				
		$tempdata = $this->get_ad_data();	
		$tempdata[$id] = $data;
		$file = ROOT_PATH . '/data/ad/ad.php'; 	
	    write_file($file, "<?php if(!defined('IN_PHPSCUP'))exit('hacking'); \nreturn " . var_export($tempdata , true) . ";\n?>");
		
		if(!is_writable($file))
		{
		exit('file directory is not writed power');//文件目录权限不可写
	    }
		 $notice = &get_message(array('msg'=>'Success!','url'=>'?app=ad','title'=>'Notice'));
		 $notice ->display();
    }

function make_ad_cache(){
	global $db,$pre,$timestamp,$webdb;
	$show.="<?php\r\n";
	$query = $db->query("SELECT * FROM `{$pre}ad_norm_place` WHERE isclose=0");
	while($rs = $db->fetch_array($query)){
		unset($_r);
		if($rs[ifsale]&&$_r=$db->get_one("SELECT u_code,u_id FROM {$pre}ad_norm_user WHERE u_endtime>'$timestamp' AND id='$rs[id]'")){
			@extract(unserialize($_r[u_code]));
		}else{
			@extract(unserialize($rs[adcode]));
		}
		
		if($rs[type]=='word'){
			$url="$webdb[www_url]/a_d/a_d_s.php?job=jump&id=$rs[id]&u_id=$_r[u_id]&url=".base64_encode($linkurl);
			$code="<SCRIPT LANGUAGE='JavaScript' src='$webdb[www_url]/a_d/a_d_s.php?job=js&ad_id=$rs[keywords]'></SCRIPT>";
			$_code="<a href='$url' target='$wordtarget'>$word</a>";
		}elseif($rs[type]=='pic'){
			$url="$webdb[www_url]/a_d/a_d_s.php?job=jump&id=$rs[id]&u_id=$_r[u_id]&url=".base64_encode($linkurl);
			$picurl=tempdir($picurl);
			$code="<SCRIPT LANGUAGE='JavaScript' src='$webdb[www_url]/a_d/a_d_s.php?job=js&ad_id=$rs[keywords]'></SCRIPT>";
			$_code="<a href='$url' target='$pictarget'><img width='$width' height='$height' src='$picurl' border=0></a>";
		}elseif($rs[type]=='swf'){
			$flashurl=tempdir($flashurl);
			$code="<SCRIPT LANGUAGE='JavaScript' src='$webdb[www_url]/a_d/a_d_s.php?job=js&ad_id=$rs[keywords]'></SCRIPT>";
			$_code="<object type='application/x-shockwave-flash' data='$flashurl' width='$width' height='$height' wmode='transparent'><param name='movie' value='$flashurl' /><param name='wmode' value='transparent' /></object>";
		}elseif($rs[type]=='duilian'){
			$lcode=$rcode='';
			if($l_src){
				$l_src=tempdir($l_src);
				if(eregi("swf$",$l_src)){
					$L_Types="flash";
				}else{
					$l_link="$webdb[www_url]/a_d/a_d_s.php?job=jump&id=$rs[id]&url=".base64_encode($l_link);
					$L_Types="photo";
				}
			}
			if($r_src){
				$r_src=tempdir($r_src);
				if(eregi("swf$",$r_src)){
					$R_Types="flash";
				}else{
					$r_link="$webdb[www_url]/a_d/a_d_s.php?job=jump&id=$rs[id]&url=".base64_encode($r_link);
					$R_Types="photo";
				}
			}
			$code="<SCRIPT LANGUAGE='JavaScript' src='$webdb[www_url]/a_d/a_d_s.php?job=js&ad_id=$rs[keywords]'></SCRIPT>";
			$_code="<script language=\"javascript\" type=\"text/javascript\">
   var adLeftSrc = \"$l_src\"   //图片地址
   var adLeftFlash = \"$L_Types\"
   var adLeftHref = \"$l_link\"
   var adLeftWidth = '$l_width'
   var adLeftHeight = '$l_height'
   var adRightSrc = \"$r_src\"//图片地址
   var adRightFlash = \"$R_Types\"
   var adRightHref = \"$r_link\"
   var adRightWidth = '$r_width'
   var adRightHeight = '$r_height'
   var marginTop = 300 //在这里更改距离浏览器底端的高度
   var marginLeft = 5//在这里更改距离浏览器右端的高度
   var navUserAgent = navigator.userAgent
   function load(){
    judge();
    move();
   }
   function move() {
    judge();
    setTimeout(\"move();\",80)
   }
   function judge(){
    if (navUserAgent.indexOf(\"Firefox\") >= 0 || navUserAgent.indexOf(\"Opera\") >= 0) {
     if (adLeftSrc != \"\") {document.getElementById(\"adLeftFloat\").style.top = (document.body.scrollTop?document.body.scrollTop:document.documentElement.scrollTop) + ((document.body.clientHeight > document.documentElement.clientHeight)?document.documentElement.clientHeight:document.body.clientHeight) - adLeftHeight - marginTop + 'px';}
     if (adRightSrc != \"\") {
      document.getElementById(\"adRightFloat\").style.top = (document.body.scrollTop?document.body.scrollTop:document.documentElement.scrollTop) + ((document.body.clientHeight > document.documentElement.clientHeight)?document.documentElement.clientHeight:document.body.clientHeight) - adRightHeight - marginTop + 'px';
      document.getElementById(\"adRightFloat\").style.left = ((document.body.clientWidth > document.documentElement.clientWidth)?document.body.clientWidth:document.documentElement.clientWidth) - adRightWidth - marginLeft + 'px';
     } 
    }
    else{
     if (adLeftSrc != \"\") {document.getElementById(\"adLeftFloat\").style.top = (document.body.scrollTop?document.body.scrollTop:document.documentElement.scrollTop) + ((document.documentElement.clientHeight == 0)?document.body.clientHeight:document.documentElement.clientHeight) - adLeftHeight - marginTop + 'px';}
     if (adRightSrc != \"\") {
      document.getElementById(\"adRightFloat\").style.top = (document.body.scrollTop?document.body.scrollTop:document.documentElement.scrollTop) + ((document.documentElement.clientHeight == 0)?document.body.clientHeight:document.documentElement.clientHeight) - adRightHeight - marginTop + 'px';
      document.getElementById(\"adRightFloat\").style.left = ((document.documentElement.clientWidth == 0)?document.body.clientWidth:document.documentElement.clientWidth) - adRightWidth - marginLeft + 'px';
     }
    }
    if (adLeftSrc != \"\") {document.getElementById(\"adLeftFloat\").style.left = marginLeft + 'px';}
   }
   
   
   
if (adLeftSrc != \"\") {
	if (adLeftFlash == \"flash\") {
		document.write(\"<div id=\\\"adLeftFloat\\\" style=\\\"position: absolute;width:\" + adLeftWidth + \";height:100px; font-size:12px;\\\"><div id='left_top'><img src=\\\"$webdb[www_url]/images/default/close.gif\\\" width=\\\"12\\\" border=\\\"0\\\" height=\\\"12\\\" onMousedown=\\\"javascript:close_ad('left_top');void(0);\\\";>\");
    
		document.write(\"<div id=\\\"Float\\\" style=\\\"width:\" + adLeftWidth + \";\\\"><embed src=\\\"\" + adLeftSrc + \"\\\" quality=\\\"high\\\"  width=\\\"\" + adLeftWidth + \"\\\" height=\\\"\" + adLeftHeight + \"\\\" type=\\\"application/x-shockwave-flash\\\"></embed></div></div>\");
		document.write(\"</div>\");
    }else{
		document.write(\"<div id=\\\"adLeftFloat\\\" style=\\\"position: absolute;width:\" + adLeftWidth + \";height:100px; font-size:12px;\\\"><img src=\\\"$webdb[www_url]/images/default/close.gif\\\" width=\\\"12\\\" border=\\\"0\\\" height=\\\"12\\\" onMousedown=\\\"javascript:close_float_left();void(0);\\\";>\");
    
		document.write(\"<div id=\\\"Float\\\" style=\\\"width:\" + adLeftWidth + \";\\\"><a href=\\\"\" + adLeftHref +\"\\\" target=_blank><img src=\\\"\" + adLeftSrc + \"\\\"  width=\\\"\" + adLeftWidth + \"\\\" height=\\\"\" + adLeftHeight + \"\\\"  border=\\\"0\\\" \></a></div>\");
		document.write(\"</div>\");
    }
}
   
   
   
if (adRightSrc != \"\") {
	if (adRightFlash == \"flash\") {
		document.write(\"<div id=\\\"adRightFloat\\\" style=\\\"position: absolute;width:\" + adRightWidth + \";height:100px; font-size:12px;text-align:right;\\\"><div id='right_top'><img src=\\\"$webdb[www_url]/images/default/close.gif\\\" width=\\\"12\\\" border=\\\"0\\\" height=\\\"12\\\" onMousedown=\\\"javascript:close_ad('right_top');void(0);\\\";>\");
    
		document.write(\"<div id=\\\"Float\\\" style=\\\"width:\" + adRightWidth + \";\\\"><embed src=\\\"\" + adRightSrc + \"\\\" quality=\\\"high\\\"  width=\\\"\" + adLeftWidth + \"\\\" height=\\\"\" + adRightHeight + \"\\\" type=\\\"application/x-shockwave-flash\\\"></embed></div></div>\");
		document.write(\"</div>\");
    }else{
		document.write(\"<div id=\\\"adRightFloat\\\" style=\\\"position: absolute;width:\" + adRightWidth + \";height:100px; font-size:12px;text-align:right;\\\"><img src=\\\"$webdb[www_url]/images/default/close.gif\\\" width=\\\"12\\\" border=\\\"0\\\" height=\\\"12\\\" onMousedown=\\\"javascript:close_float_right();void(0);\\\";>\");
    
		document.write(\"<div id=\\\"Float\\\" style=\\\"width:\" + adRightWidth + \";\\\"><a href=\\\"\" + adRightHref +\"\\\" target=_blank><img src=\\\"\" + adRightSrc + \"\\\"   width=\\\"\" + adLeftWidth + \"\\\" height=\\\"\" + adRightHeight + \"\\\"  border=\\\"0\\\"  \></a></div>\");
		document.write(\"</div>\");
    }
}
load();
function close_float_right(){
	document.getElementById(\"adRightFloat\").style.display=\"none\";
} 
function close_float_left(){
	document.getElementById(\"adLeftFloat\").style.display=\"none\";
}
function close_ad(d){
	document.getElementById(d).style.display=\"none\";
}
</script>";
		}else{
			if(!eregi("<SCRIPT",$code)){
				$_code=$code;
				$code="<SCRIPT LANGUAGE='JavaScript' src='$webdb[www_url]/a_d/a_d_s.php?job=js&ad_id=$rs[keywords]'></SCRIPT>";
			}else{
				$_code=$code;
			}
		}
		if(($rs[begintime]&&$rs[begintime]>$timestamp)||($rs[endtime]&&$timestamp>$rs[endtime]))
		{
			$code=$_code='';
		}
		//必须使用'号,当作是字符串,不能使用"号,防止用户放$变量
		$show.="\$AD_label['$rs[keywords]']=stripslashes('".addslashes($code)."');\r\n";
		$show.="\$_AD_label['$rs[keywords]']=stripslashes('".addslashes($_code)."');\r\n";
		$code=$_code='';
	}
	write_file(ROOT_PATH."data/ad_cache.php",$show);
}

  
}

?>